//
//  SSBPopupView.m
//  tast
//
//  Created by 合事佬 on 2018/5/24.
//  Copyright © 2018年 合事佬. All rights reserved.
//

#import "SSBPopupView.h"

@implementation SSBPopupView

+ (instancetype)sharePopupView{
    
    static id managerInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        managerInstance = [[[self class] alloc] init];
    });
    return managerInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

+ (void)PopupShowLoadingView:(UIView *)View backViewBool:(BOOL)backViewBool bezelViewColor:(UIColor *)bezelViewColor contenColor:(UIColor *)contenColor userInteraction:(BOOL)userInteraction{
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.contentColor = contenColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
}

+ (void)PopupShowLoadingLabelView:(UIView *)View backViewBool:(BOOL)backViewBool labelText:(NSString *)labelText labelFont:(UIFont *)labelFont bezelViewColor:(UIColor *)bezelViewColor contenColor:(UIColor *)contenColor userInteraction:(BOOL)userInteraction{
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.label.text = labelText;
    [SSBPopupView sharePopupView].hud.label.font = labelFont;
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.contentColor = contenColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
}

+ (void)PopupShowLoadingDetailsLabelView:(UIView *)View backViewBool:(BOOL)backViewBool labelText:(NSString *)labelText labelFont:(UIFont *)labelFont detailsLabelText:(NSString *)detailsLabelText detailsLabelFont:(UIFont *)detailsLabelFont bezelViewColor:(UIColor *)bezelViewColor contenColor:(UIColor *)contenColor userInteraction:(BOOL)userInteraction{
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.label.text = labelText;
    [SSBPopupView sharePopupView].hud.label.font = labelFont;
    [SSBPopupView sharePopupView].hud.detailsLabel.text = detailsLabelText;
    [SSBPopupView sharePopupView].hud.detailsLabel.font = detailsLabelFont;
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.contentColor = contenColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
}

+ (void)PopupShowDeterminateView:(UIView *)View backViewBool:(BOOL)backViewBool labelText:(NSString *)labelText lableFont:(UIFont *)labelFont bezelViewColor:(UIColor *)bezelViewColor contenColor:(UIColor *)contenColor userInteraction:(BOOL)userInteraction{
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.mode = MBProgressHUDModeDeterminate;
    [SSBPopupView sharePopupView].hud.label.text = labelText;
    [SSBPopupView sharePopupView].hud.label.font = labelFont;
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.contentColor = contenColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
    
    [[SSBPopupView sharePopupView] doSomeWorkWithProgress:View];
}

+ (void)PopupShowAnnularDeterminateView:(UIView *)View backViewBool:(BOOL)backViewBool labelText:(NSString *)labelText labelFont:(UIFont *)labelFont bezelViewColor:(UIColor *)bezelViewColor contenColor:(UIColor *)contenColor userInteraction:(BOOL)userInteraction{
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.mode = MBProgressHUDModeAnnularDeterminate;
    [SSBPopupView sharePopupView].hud.label.text = labelText;
    [SSBPopupView sharePopupView].hud.label.font = labelFont;
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.contentColor = contenColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
    
    [[SSBPopupView sharePopupView] doSomeWorkWithProgress:View];
}

+ (void)PopupShowBarDeterminateView:(UIView *)View backViewBool:(BOOL)backViewBool labelText:(NSString *)labelText labelFont:(UIFont *)labelFont bezelViewColor:(UIColor *)bezelViewColor contenColor:(UIColor *)contenColor userInteraction:(BOOL)userInteraction{
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    [SSBPopupView sharePopupView].hud.label.text = labelText;
    [SSBPopupView sharePopupView].hud.label.font = labelFont;
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.contentColor = contenColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
    [[SSBPopupView sharePopupView] doSomeWorkWithProgress:View];
}

+ (void)PopupShowTextView:(UIView *)View backViewBool:(BOOL)backViewBool labelText:(NSString *)labelText labelFont:(UIFont *)labelFont labelColor:(UIColor *)labelColor bezelViewColor:(UIColor *)bezelViewColor userInteraction:(BOOL)userInteraction{
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.mode = MBProgressHUDModeText;
    [SSBPopupView sharePopupView].hud.label.text = labelText;
    [SSBPopupView sharePopupView].hud.label.font = labelFont;
    [SSBPopupView sharePopupView].hud.label.textColor = labelColor;
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    [SSBPopupView sharePopupView].hud.center = View.center;
    
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
}

+ (void)PopupShowCustomView:(UIView *)View backViewBool:(BOOL)backViewBool labelText:(NSString *)labelText labelFont:(UIFont *)labelFont labelColor:(UIColor *)labelColor imageName:(NSString *)imageName bezelViewColor:(UIColor *)bezelViewColor userInteraction:(BOOL)userInteraction{
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.mode = MBProgressHUDModeCustomView;
    UIImage *image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [SSBPopupView sharePopupView].hud.customView = [[UIImageView alloc] initWithImage:image];
    [SSBPopupView sharePopupView].hud.square = YES;
    [SSBPopupView sharePopupView].hud.label.text = labelText;
    [SSBPopupView sharePopupView].hud.label.font = labelFont;
    [SSBPopupView sharePopupView].hud.label.textColor = labelColor;
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
}

+ (void)PopupShowCustomAnimationView:(UIView *)View backViewBool:(BOOL)backViewBool labelText:(NSString *)labelText labelFont:(UIFont *)labelFont labelColor:(UIColor *)labelColor withImgArry:(NSArray *)withImgArry bezelViewColor:(UIColor *)bezelViewColor userInteraction:(BOOL)userInteraction{
    
    UIImageView *showImageView = [[UIImageView alloc] init];
    showImageView.animationImages = withImgArry;
    [showImageView setAnimationRepeatCount:0];
    [showImageView setAnimationDuration:(withImgArry.count + 1) * 0.075];
    [showImageView startAnimating];
    
    [[SSBPopupView sharePopupView]existingCartridgeBoxDisappears];
    
    [SSBPopupView sharePopupView].hud = [MBProgressHUD showHUDAddedTo:View animated:YES];
    [SSBPopupView sharePopupView].hud.mode = MBProgressHUDModeCustomView;
    [SSBPopupView sharePopupView].hud.customView = showImageView;
    [SSBPopupView sharePopupView].hud.label.text = labelText;
    [SSBPopupView sharePopupView].hud.label.font = labelFont;
    [SSBPopupView sharePopupView].hud.label.textColor = labelColor;
    [SSBPopupView sharePopupView].hud.bezelView.backgroundColor = bezelViewColor;
    [SSBPopupView sharePopupView].hud.userInteractionEnabled = userInteraction;
    
    if (backViewBool == YES){
        [SSBPopupView sharePopupView].hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        [SSBPopupView sharePopupView].hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.7f];
    }
}


+(void)Popupdismiss:(BOOL)hideBool timeDelay:(int)timeDelay{
    if ([SSBPopupView sharePopupView].hud != nil) {
        [[SSBPopupView sharePopupView].hud hideAnimated:hideBool afterDelay:timeDelay];
    }
}

- (void)existingCartridgeBoxDisappears{
    if ([SSBPopupView sharePopupView].hud != nil) {
        [[SSBPopupView sharePopupView].hud hideAnimated:YES];
        [SSBPopupView sharePopupView].hud = nil;
    }
}

- (void)doSomeWorkWithProgress:(UIView *)View{
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        BOOL canceled = NO;
        float progress = 0.0f;
        while (progress < 1.0f) {
            if (canceled) break;
            progress += 0.01f;
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD HUDForView:View].progress = progress;
            });
            usleep(50000);
        }
    });
}

@end
